/**
* @description: 剑指 Offer 36. 二叉搜索树与双向链表
* @author hewei
* @date 2022/7/21 15:39
* @version 1.0
*/

public class TreeToDoublyList {

    Node dummyHead;
    Node endNode;
    public Node treeToDoublyList(Node root) {
        if (root == null) return null;
        dummyHead = new Node();
        endNode = dummyHead;
        order(root);
        dummyHead.right.left = endNode;
        endNode.right = dummyHead.right;
        return dummyHead.right;
    }

    public void order(Node node) {
        if (node == null) return;
        order(node.left);
        endNode.right = node;
        node.left = endNode;
        endNode = node;
        order(node.right);
    }
}
